# 记住喜欢的数 　将练习10-11中的程序合二为一。
# 如果存储了用户喜欢的数，就向用户显示它，否则提示用户输
# 入喜欢的数并将其存储到文件中。运行这个程序两次，看看它
# 能否像预期的那样工作。

import json
def favorite_number():
	"""如果存储了用户喜欢的数，就向用户显示它，否则提示用户输入喜欢的数并将其存储到文件中"""

	try:
		filename = 'favorite_number1.json'
		with open(filename) as f:
			favorite_number = json.load(f)
	except FileNotFoundError:
		favorite_number = None
	else:
		print(f"I know your favorite number! It's {favorite_number}.")
	
	if favorite_number == None:
		favorite_number = input("What is your favorite number? ")
		with open(filename, 'w') as f:
			json.dump(favorite_number, f)
			print(f"I will remember your favorite number! It's {favorite_number}.")


favorite_number()

